/**管理员数据管理 */
import { defineStore } from "pinia";
import {
    getAdminList,
    getAdminTotal,
    addAdmin,
    editAdmin,
    delAdmin
} from '../api/adminApi'

import { ElMessage } from "element-plus";
// 注册store
const useAdminStore = defineStore("admins", {
    state: () => {
        return {
            // 页码
            page: 1,
            // 单页展示数据
            size: 10,
            // 总数
            total: 0,
            // 管理员数据
            adminList: []
        }
    },
    getters: {

    },
    actions: {
        GET_ADMINLIST(list) {
            this.adminList = list
        },
        CHANGE_PAGE(page) {
            this.page = page
        },
        CHANGE_SIZE(size) {
            this.size = size
        },
        CHANGE_TOTAL(total) {
            this.total = total
        },

        // 分页获取数据
        getAdminListAction() {
            getAdminList(this.page, this.size).then(response => {
                if (response.code === 200) {
                    console.log(response, "admin list")
                    this.GET_ADMINLIST(response.list)
                }
            })
        },
        // 获取管理员总数
        getAdminTotalAction() {
            getAdminTotal().then(response => {
                if (response.code === 200) {
                    console.log(response, "admin total")
                    this.CHANGE_TOTAL(response.list[0].total)
                }
            })
        },
        // 新增
        addAdminAction(adminForm) {
            addAdmin(adminForm).then(response => {
                if (response.code === 200) {
                    ElMessage({
                        type: 'success',
                        message: "管理员添加成功"
                    })
                    this.getAdminListAction()
                    this.getAdminTotalAction()
                } else {
                    ElMessage({
                        type: "error",
                        message: res.msg,
                    });
                }
            })
        },
        // 编辑
        editAdminAction(adminForm) {
            editAdmin(adminForm).then(response => {
                if (response.code === 200) {
                    ElMessage({
                        type: 'success',
                        message: "管理员编辑成功"
                    })
                    this.getAdminListAction()
                } else {
                    ElMessage({
                        type: "error",
                        message: res.msg,
                    });
                }
            })
        },
        // 删除
        delAdminAction(id) {
            delAdmin(id).then(response => {
                console.log('response', response)
                if (response.code === 200) {
                    ElMessage({
                        type: 'success',
                        message: "管理员删除成功"
                    })
                    this.getAdminListAction()
                    this.getAdminTotalAction()
                } else {
                    ElMessage({
                        type: "error",
                        message: response.msg,
                    });
                }
            })
        },
    }
})
// 导出
export default useAdminStore